package simonby.top.annualring.application.service;

import simonby.top.annualring.domain.model.FamilyMember;
import simonby.top.annualring.domain.repository.FamilyMemberRepository;
import simonby.top.annualring.infrastructure.repository.FamilyMemberRepositoryImpl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 家族成员应用服务
 * 处理家族成员相关的业务逻辑
 */
@Service
public class FamilyMemberApplicationService {
    private final FamilyMemberRepository familyMemberRepository;

    public FamilyMemberApplicationService(FamilyMemberRepositoryImpl familyMemberRepository) {
        this.familyMemberRepository = familyMemberRepository;
    }

    /**
     * 创建家族成员
     * @param member 家族成员对象
     * @return 创建后的家族成员
     */
    public FamilyMember createFamilyMember(FamilyMember member) {
        return familyMemberRepository.save(member);
    }

    /**
     * 根据ID获取家族成员
     * @param id 成员ID
     * @return 家族成员对象
     */
    public Optional<FamilyMember> getFamilyMemberById(Long id) {
        return familyMemberRepository.findById(id);
    }

    /**
     * 根据家族树ID获取所有成员
     * @param familyTreeId 家族树ID
     * @return 家族成员列表
     */
    public List<FamilyMember> getFamilyMembersByFamilyTreeId(Long familyTreeId) {
        return familyMemberRepository.findByFamilyTreeId(familyTreeId);
    }

    /**
     * 获取所有家族成员
     * @return 家族成员列表
     */
    public List<FamilyMember> getAllFamilyMembers() {
        return familyMemberRepository.findAll();
    }

    /**
     * 更新家族成员
     * @param member 家族成员对象
     * @return 更新后的家族成员
     */
    public FamilyMember updateFamilyMember(FamilyMember member) {
        return familyMemberRepository.save(member);
    }

    /**
     * 删除家族成员
     * @param id 成员ID
     */
    public void deleteFamilyMember(Long id) {
        familyMemberRepository.deleteById(id);
    }

    /**
     * 设置父子关系
     * @param childId 子成员ID
     * @param fatherId 父成员ID
     * @return 更新后的子成员
     */
    public Optional<FamilyMember> setFatherRelation(Long childId, Long fatherId) {
        Optional<FamilyMember> childOpt = familyMemberRepository.findById(childId);
        Optional<FamilyMember> fatherOpt = familyMemberRepository.findById(fatherId);
        
        if (childOpt.isPresent() && fatherOpt.isPresent()) {
            FamilyMember child = childOpt.get();
            child.setFatherId(fatherId);
            return Optional.of(familyMemberRepository.save(child));
        }
        
        return Optional.empty();
    }

    /**
     * 设置母子关系
     * @param childId 子成员ID
     * @param motherId 母成员ID
     * @return 更新后的子成员
     */
    public Optional<FamilyMember> setMotherRelation(Long childId, Long motherId) {
        Optional<FamilyMember> childOpt = familyMemberRepository.findById(childId);
        Optional<FamilyMember> motherOpt = familyMemberRepository.findById(motherId);
        
        if (childOpt.isPresent() && motherOpt.isPresent()) {
            FamilyMember child = childOpt.get();
            child.setMotherId(motherId);
            return Optional.of(familyMemberRepository.save(child));
        }
        
        return Optional.empty();
    }

    /**
     * 设置配偶关系
     * @param memberId1 成员1 ID
     * @param memberId2 成员2 ID
     * @return 更新后的成员1
     */
    public Optional<FamilyMember> setSpouseRelation(Long memberId1, Long memberId2) {
        Optional<FamilyMember> member1Opt = familyMemberRepository.findById(memberId1);
        Optional<FamilyMember> member2Opt = familyMemberRepository.findById(memberId2);
        
        if (member1Opt.isPresent() && member2Opt.isPresent()) {
            FamilyMember member1 = member1Opt.get();
            FamilyMember member2 = member2Opt.get();
            
            member1.setSpouseId(memberId2);
            member2.setSpouseId(memberId1);
            
            familyMemberRepository.save(member2); // 先保存成员2
            return Optional.of(familyMemberRepository.save(member1)); // 返回成员1
        }
        
        return Optional.empty();
    }

    /**
     * 获取成员的直接亲属（父亲、母亲、配偶、子女）
     * @param memberId 成员ID
     * @return 直接亲属列表
     */
    public List<FamilyMember> getDirectRelatives(Long memberId) {
        Optional<FamilyMember> memberOpt = familyMemberRepository.findById(memberId);
        if (memberOpt.isPresent()) {
            FamilyMember member = memberOpt.get();
            Long familyTreeId = member.getFamilyTreeId();
            
            // 查找该成员的父母、配偶和子女
            List<FamilyMember> relatives = familyMemberRepository.findByFamilyTreeId(familyTreeId);
            return relatives.stream()
                    .filter(relative -> 
                        (relative.getId().equals(member.getFatherId())) ||     // 父亲
                        (relative.getId().equals(member.getMotherId())) ||     // 母亲
                        (relative.getId().equals(member.getSpouseId())) ||     // 配偶
                        (relative.getFatherId() != null && relative.getFatherId().equals(memberId)) ||  // 儿子
                        (relative.getMotherId() != null && relative.getMotherId().equals(memberId)) ||  // 女儿
                        (relative.getSpouseId() != null && relative.getSpouseId().equals(memberId))     // 配偶
                    )
                    .collect(java.util.stream.Collectors.toList());
        }
        
        return List.of();
    }
}